package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝DTO
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获得用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectShoppingCart(shoppingCart);

        //这里是传入商品对象 然后判断 用户id等于表的用户id  并且如果有参数则加参数
        if (shoppingCarts != null && shoppingCarts.size() == 1) {
            for (ShoppingCart cart : shoppingCarts) {
                cart.setNumber(cart.getNumber() + 1);
                shoppingCartMapper.updateShoppingCart(cart);
            }
        } else {
            if (shoppingCart.getDishId() != null && shoppingCart.getSetmealId() == null) {
                Dish dish = dishMapper.selectDishById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insertShoppingCart(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> selectShoppingCart() {
        return shoppingCartMapper.selectShoppingCart(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        if (shoppingCartDTO.getDishId() != null) {
            ShoppingCart shoppingCartDish = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCartDish);
            shoppingCartDish.setUserId(BaseContext.getCurrentId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectShoppingCart(shoppingCartDish);
            if (shoppingCarts != null) {
                if (shoppingCarts.get(0).getNumber() > 1) {
                    ShoppingCart shoppingCart = shoppingCarts.get(0);
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateShoppingCart(shoppingCart);
                } else {
                    shoppingCartMapper.deleteShoppingCart(shoppingCarts.get(0));
                }
            }
        }

        if (shoppingCartDTO.getSetmealId() != null) {
            ShoppingCart shoppingCartSetmeal = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCartSetmeal);
            shoppingCartSetmeal.setUserId(BaseContext.getCurrentId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectShoppingCart(shoppingCartSetmeal);
            if (shoppingCarts != null) {
                if (shoppingCarts.get(0).getNumber() > 1) {
                    ShoppingCart shoppingCart = shoppingCarts.get(0);
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateShoppingCart(shoppingCart);
                } else {
                    shoppingCartMapper.deleteShoppingCart(shoppingCarts.get(0));
                }
            }

        }

    }

    @Override
    public void deleteAllShoppingCart() {
        shoppingCartMapper.deleteShoppingCart(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }
}
